package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public User registerIfUserNotExists(String phone) {

        //根据手机号查询用户是否存在
        LambdaQueryWrapper<User> qw=new LambdaQueryWrapper<>();
        qw.eq(User::getPhone,phone);
        User user = getOne(qw);
        //如果用户不存在，则需要注册用户(将用户添加到数据库中)
        if (user==null){
            user=new User();
            user.setPhone(phone);
            save(user);
        }
        return user;
    }
}
